<?php

$objXajax->registerFunction("frmmenu_initPage");
$objXajax->registerFunction("frmmenu_loadGrid");
$objXajax->registerFunction("frmmenu_save");
$objXajax->registerFunction("frmmenu_edit");
  
function frmmenu_initPage(){
  global $objDbUpdate,$strCheck;
  $objResponse = new xajaxResponse();
  $strCheck = frmmenu_createTblHTMLPower(1);
  $objResponse->addAssign('frmmenu_Powers', 'innerHTML', $strCheck);
  return    $objResponse->getXML();             
}
function frmmenu_save($params){
 global $objDbUpdate;
 $objResponse = new xajaxResponse(); 
 $name_vi = addslashes($params['txtViMenu']);
 $name_en = addslashes($params['txtEnMenu']);
 $icon = addslashes($params['txtIcon']);
 $ordershow = $params['txtOrdershow'];
 $edit = $params['hdEdit'];
 $id = $params['hdId'];
 $arrPower =  $params['arrPower'];
//echo '<pre>'; print_r($params); die();
 if($edit!=1){
   $sqlInsert = "INSERT INTO tbl_main_menu(name_vi,name_en,icon,active,ordershow) VALUE('$name_vi','$name_en','$icon','0','$ordershow')";
   
   $result    =  $objDbUpdate->Execute($sqlInsert);
     $arrPower  = $params['arrPower'];
     $id_insert = mysql_insert_id();
     $strValue = frmmenu_createValue($params,$id_insert);
     if(strlen($strValue)>0){
        $sqlInsert = "INSERT INTO tbl_mainmenu_powertype(main_menu_id,power_type_id) VALUES$strValue"; 
        $objDbUpdate->Execute($sqlInsert);
     }
     $objResponse->addScript("changeUpdate('frmmenu',$id_insert);");     
 }else{
     $sql = "UPDATE tbl_main_menu SET name_vi ='$name_vi',name_en ='$name_en',icon ='$icon',ordershow='$ordershow' WHERE id = '$id'";
    if($objDbUpdate->Execute($sql)){
        $sql = "DELETE FROM tbl_mainmenu_powertype WHERE main_menu_id = '$id'";
        if($objDbUpdate->Execute($sql)){
             $strValue = frmmenu_createValue($params,$id);
         if(strlen($strValue)>0){
            $sqlInsert = "INSERT INTO tbl_mainmenu_powertype(main_menu_id,power_type_id) VALUES$strValue"; 
           // echo $sqlInsert; die();
            $objDbUpdate->Execute($sqlInsert);
         }      
            
        }
    }
     
 }
 $objResponse->addScript('xajax_frmmenu_loadGrid();');
 $objResponse->addScript('changeWaiting(1);');
 return   $objResponse->getXML($sqlInsert);
}

function frmmenu_createValue($params,$id_insert){
     $result = '';
     foreach($params as $key=>$val){
         if(substr($key,0,8)=='chkpower' && $val==1){
            $val1 =substr($key,8) ;
            $result .= ",('$id_insert','$val1')"; 
         }
     }
     $result = strlen($result)>0?substr($result,1):'';
     return $result;
 }
 
function frmmenu_createOptionFather(){
global $objDbSelect,$lang;
  $sql = "SELECT id AS id, name_$lang"."_menu as name from tbl_main_menu  ";
  $arrfather = $objDbSelect->GetArray($sql);
  $temp ='';
  $count = count($arrfather);
  for($i=0;$i<$count;$i++){
    $id = $arrfather[$i]['id'];
    $name = $arrfather[$i]['name'];
    $temp.= "<option value='$id'> $name </option>"  ;
  }
  return $temp;
}

function frmmenu_getOrderByFather($father_id){
  global $objDbSelect;
  $sql = "SELECT category_order AS cateorder from tbl_menu WHERE id = $father_id";
  $arrOrder = $objDbSelect->GetArray($sql);
  if(count($arrOrder)>0){
      return $arrOrder[0]['cateorder'] ;
  }
  return 1;
}

function frmmenu_createTblHTMLPower($numCol){
  global $objDbSelect;
  $lang = $_SESSION['lang'];
  $sql = "select id,name_$lang as name from tbl_power_type";

  $arrMenu = $objDbSelect->GetArray($sql);
      
  $count = count($arrMenu);
  $numadd = ($numCol- $count%$numCol)%$numCol;
  for($j=0;$j<$numadd;$j++){
      $arrPustemp= array();
      $arrPustemp[$count+$j-1] = array('id'=>0,'name'=>'');
      $arrMenu=array_merge($arrMenu,$arrPustemp);
  }
  $count = count($arrMenu);
  $tbl ="<table id='tblpower' style='padding-left:10px;background:transparent' class='width100'>";
  for($i=0;$i<$count;$i++){
      $id = $arrMenu[$i]['id'];
      $name = $arrMenu[$i]['name'];
      $tdTemp='';
      if($id!=0){
       $tdTemp = "<td style='margin-left:10px;padding-left:10px;'><input type='checkbox'  class='chkbox' onchange=\"fn_SubChkChange('frmmenu_chkAllPower','frmmenu_chkpower')\" checked='checked'  id='frmmenu_chkpower$id' value='$id' name='frmmenu_chkpower'  />$name</td>";   
      }else{
         $tdTemp ='<td></td>'; 
      }      
      if($i%$numCol==0){
        $tbl .= "<tr>$tdTemp";  
      }else if($i%$numCol==$numCol-1){
        $tbl .= "$tdTemp</tr>";  
      }else{
         $tbl .= "$tdTemp"; 
      }
  }
  return $tbl.'</table>';
}

function frmmenu_loadGrid(){     
global $lang,$objDbSelect,$path;
$lang ='vi';
$arrMenu = frmmenu_GetMenu();
$objResponse = new xajaxResponse();
$oTbs = new clsTinyButStrong();
$oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/hotel_menu_grid.html');
$oTbs->MergeBlock('blk_data', $arrMenu);
$oTbs->Show(TBS_NOTHING);
$objResponse->addAssign('frmmenu_divGrid', 'innerHTML', $oTbs->Source); 
$objResponse->addScriptCall("dataTableObj.CreatDataTable('frmmenu_tbl');");                             
$objResponse->addScript('changeWaiting(1);');  
return $objResponse->getXML();
}

function  frmmenu_GetMenu(){
global $objDbSelect;
$sqlSelect = 'select * from tbl_main_menu ';
return $objDbSelect->GetArray($sqlSelect);
}

function  frmmenu_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_main_menu where id = '$id'";
    $arrMenu         = $objDbSelect->GetArray($sql);
    
     $sql         = "select * from tbl_mainmenu_powertype where main_menu_id = '$id'";
     $arrPower         = $objDbSelect->GetArray($sql);            
    $name_vi = $arrMenu[0]['name_vi'];
    $name_en = $arrMenu[0]['name_en'];
    $icon    = $arrMenu[0]['icon'];
    $ordershow    = $arrMenu[0]['ordershow'];
    $strId = '';
    for($i=0;$i<count($arrPower);$i++){
      $strId .= ','.$arrPower[$i]['power_type_id'] ; 
    }
    if(strlen($strId)>0){
        $strId= substr($strId,1);
    }
    $objResponse->addScript("setPower('$strId');");
    
    $objResponse->addScript("$('#frmmenu_txtViMenu').val('$name_vi');");
    $objResponse->addScript("$('#frmmenu_txtEnMenu').val('$name_en');");
    $objResponse->addScript("$('#frmmenu_txtIcon').val('$icon');");
    $objResponse->addScript("$('#frmmenu_txtOrdershow').val('$ordershow');");
    $objResponse->addScript("fn_SubChkChange('frmmenu_chkAllPower','frmmenu_chkpower');");    
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
} 
  
  # end region process data
?>
